/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ccs.util;

import java.util.Properties;
import java.util.ResourceBundle;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

/**
 *
 * @author José Irineo Coria Hernández
 */
public class Mail {

    private static String smtp;
    private static String service;
    private static String user;
    private static String userName;
    private static String password;
    private static String auth;
    private static String starttls;
    private static ResourceBundle propertiesMail;
    private static Properties props;
/**
 * Método que sirve para enviar correos.
 * @param addressee la dirección de correo destino.
 * @param subject asunto para el correo.
 * @param text contenido del correo, puede ser codigo html para una mejor presentacion.
 * @return <ul><li><code>true</code> si la operación es exitosa.</li><li><code>false</code> si la operación falló.</li></ul>
 */
    public static boolean send(String addressee, String subject, String text) {
        boolean result = false;
        if (propertiesMail == null) {
            loadBundle();
        }
        if (props == null) {
            setProperties();
        }
        Transport t = null;
        try {
            //preparamos la sesion
            Session session = Session.getDefaultInstance(props);

            // Construimos el mensaje
            MimeMessage message = new MimeMessage(session);
            message.setFrom(new InternetAddress(userName + " " + "<" + user + ">"));
            message.addRecipient(Message.RecipientType.TO, new InternetAddress(addressee));
            message.setSubject(subject);
            //            message.setText(texto);    // enviar texto plano
            message.setContent(text, "text/html"); //enviar codigo html

            // Lo enviamos.
            t = session.getTransport("smtp");
            t.connect(user, password);
            t.sendMessage(message, message.getAllRecipients());
            result=true;
        } catch (Exception ex) {
            System.out.println("Error [Mail - send]:\n\t"+ex.getMessage());
            result=false;
        }finally{
            if(t!=null){
                try {
                    t.close();
                } catch (MessagingException ex) {}
            }
        }
        return result;
    }

    private static void loadBundle() {
        propertiesMail = ResourceBundle.getBundle("mail");
        smtp = propertiesMail.getString("smtp");
        service = propertiesMail.getString("service");
        user = propertiesMail.getString("user");
        userName = propertiesMail.getString("userName");
        password = propertiesMail.getString("password");
        auth = propertiesMail.getString("auth");
        starttls = propertiesMail.getString("starttls");
    }

    private static void setProperties() {
        props = new Properties();
        props.setProperty("mail.smtp.host", smtp);
        props.setProperty("mail.smtp.starttls.enable", starttls);
        props.setProperty("mail.smtp.port", service);
        props.setProperty("mail.smtp.user", user);
        props.setProperty("mail.smtp.auth", auth);
    }

}
